<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class GetContentController extends Controller
{
	/**
	 * 抓取图片
	 */
    public function getimgs($url)
    {
    	$ch= curl_init($url);

    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	curl_setopt($ch,CURLOPT_HTTPHEADER,[
    		'Host: item.yhd.com',
    		'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0',
    		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    		'Referer: http://list.yhd.com/c28115-0/b956837-4479?tp=15.64691735.561.0.4.LXmqopv-10-FJLeo&ti=UAPAFb',
    		'Connection: close'
    		]);
    	curl_setopt($ch,CURLOPT_TIMEOUT,10);

    	$res = curl_exec($ch);
		preg_match_all('/<b><img width.*src="(.*)" .*b>/isU',$res,$imgs);
		return $imgs[1];	
    }
    /**
     * 抓取详情
     */
    public function getdetail($url)
    {
    	// $url='http://item.yhd.com/item/64691735?tc=3.0.5.64691735.42&tp=51.%E5%A5%B3%E8%A3%85%20%E7%A7%8B.124.162.1.LXmWTx8-10-2nfuO&ti=899SFB';
    	$ch= curl_init($url);

    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	curl_setopt($ch,CURLOPT_HTTPHEADER,[
    		'Host: item.yhd.com',
    		'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0',
    		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    		'Referer: http://list.yhd.com/c28115-0/b956837-4479?tp=15.64691735.561.0.4.LXmqopv-10-FJLeo&ti=UAPAFb',
    		'Connection: close'
    		]);
    	curl_setopt($ch,CURLOPT_TIMEOUT,10);

    	$res = curl_exec($ch);
		preg_match_all('/id="J_des" data-tpa="DETAIL_PROD_DES_TAB">(.*)<div class="clearfix" id=\'buyer_experience\'>/isU',$res,$detail);
		return $detail[1][0];	
    }
    /**
     * 抓取标题
     */
    public function getTitle($url)
    {
    	$ch= curl_init($url);
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	curl_setopt($ch,CURLOPT_HTTPHEADER,[
    		'Host: item.yhd.com',
    		'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0',
    		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    		'Referer: http://list.yhd.com/c28115-0/b956837-4479?tp=15.64691735.561.0.4.LXmqopv-10-FJLeo&ti=UAPAFb',
    		'Connection: close'
    		]);
    	curl_setopt($ch,CURLOPT_TIMEOUT,10);

    	$res = curl_exec($ch);
		preg_match_all('/<h1.*>(.*)<\/h1>/isU',$res,$title);
		return $title[1][0];	
    }
    /**
     * 商品详情,价格路径
     */
    public function getprice($url)
    {
    	//根据传入的url初始化curl
    	$ch= curl_init($url);

    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	//请求头模拟
    	curl_setopt($ch,CURLOPT_HTTPHEADER,[
    		'Host: list.yhd.com',
    		'User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:50.0) Gecko/20100101 Firefox/50.0',
    		'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    		'Referer: http://list.yhd.com/c31832-0-90890/b/a-s1-v4-p1-price-d0-f0-m1-rt0-pid-mid0-k/?tc=3.0.9.31832.2&tp=52.28115.106.0.3.LXmqtF7-10-FJLeo&ti=88RM3C',
    		'Connection: close'
    		]);
    	curl_setopt($ch,CURLOPT_TIMEOUT,10);
    	//获得url的所有内容
    	$res = curl_exec($ch);
    	//声明一个空数组,用来接收路径及价格
    	$goods=[];
    	//正则匹配获得路径数组
		preg_match_all('/defaultFlag="1".*href="(.*)"/isU',$res,$urls);
		//将路径数组压入到空数组内
		$goods[]=$urls[1];
		//正则匹配获得价格
		preg_match_all('/yhdPrice="(.*)"/isU',$res,$price);
		//将价格数组压入到声明的空数组中
		$goods[]=$price[1];
		//将数组返回
		return $goods;	
    }

    /**
     * 循环抓取
     */
    public function getgoods()
    {
    	set_time_limit(0);
    	for($i=1;$i<=20;$i++){
    	//遍历url
    		$url='http://list.yhd.com/c28115-0-90896/b/a-s1-v0-p1-price-d0-f0-m1-rt0-pid-mid0-k/?tc=3.0.9.90896.1&tp=52.31832.107.0.1.LXoxNpz-10-FJLeo&ti=EVSF3J&page=1#page='.$i.'&sort=1';
    		//调用方法获取数组urls以及价格数组
    		$goods = $this->getprice($url);
    		//声明数组接收urls
    		$arr=$goods[0];
    		//遍历数组	
    		foreach($arr as $k=>$v){
    			//获取对应的商品价格
    			$table['price']=$goods[1][$k];
    			//获取商品标题
    			$table['title']= $this->getTitle($v);
    			//获取商品详情
    			$table['detail']= $this->getdetail($v);
    			//随机商品分类
    			$table['type_id'] = rand(16,22);
    			//随机商品库存数量
    			$table['store'] =rand(20,100);
    			//商品颜色
    			$table['color'] ='红色@@@蓝色@@@绿色@@@黑色@@@白色';
    			//商品的尺码
    			$table['size'] = 'S@@@M@@@L@@@XL@@@XXL@@@XXXl';
    			//商品上架时间
    			$table['time'] = time()+rand(1,100000);
    			//将内容插入商品列表并获得插入id
    			$gid=DB::table('goods')->insertGetId($table);
    			//获取图片路径(数组)
    			$imgs=$this->getimgs($v);
    			//声明空的数组
    			$goods_img=[];
    			foreach($imgs as $v){
    				//调用图片下载方法,获得新的本地路径
    				$path=$this->moveImg($v);
    				//拼接二维数组
    				$goods_img[]=['gid'=>$gid,'path'=>$path];
    			}
    			//将二维数组插入到商品图片表
    			DB::table('goods_img')->insert($goods_img);			
    		}
    	}
    }

    public function moveImg($url)
    {
    	//拼接字符串格式
    	$newurl='http:'.str_replace('_58*82','_332*464',$url);
    	//初始化curl
    	$ch= curl_init($newurl);
    	//curl返回值
    	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    	//超时时间
    	curl_setopt($ch,CURLOPT_TIMEOUT,10);
    	//获取内容
    	$res = curl_exec($ch);
    	//后缀名
    	$shuffix='.jpg';
    	//目录
    	$dir='/uploads/'.date('Y-m-d');
    	if(!is_dir($dir)){
    		mkdir($dir , 0777 , true);
    	}
    	//路径拼接
    	$path= $dir.'/'.time().rand(100000,999999).$shuffix;
    	// dd($path);
    	//将文件放入目录
    	file_put_contents('.'.$path,$res);
    	// dd($path);
    	//返回目录名称
    	return $path;
    }
}
